'use strict';

    var app = angular.module('app');
        app.controller('quickAuthController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster','$modal',
                                             function($scope, $rootScope, $state, $timeout, toaster, $modal) {
     
        $scope.record = {};
        $scope.record.authType = 0;
        $scope.record.id = $state.params.id;
        $scope.record.status = 1;
        $scope.title="快捷人员授权";
        $scope.loading = true;
        //初始化验证
        validate();
        $scope.submit= function(){
        	console.log(angular.toJson($scope.people));
        	console.log(angular.toJson($scope.record));
            var m = $scope.record;
            if(m){
                $scope.isDisabled = true;//提交disabled
                $.ajax({
    				type: 'POST',
    	            dataType: 'json',
    				contentType:'application/json;charset=UTF-8',
    				url : 'dac/authm/quick/auth',
    				data: angular.toJson($scope.record)
    			}).then(function(result){
                    if(result.httpCode ==200){//成功
                        toaster.clear('*');
                        toaster.pop('success', '', "保存成功");
                        $timeout(function(){
                            $state.go('main.authm.cardAuth.list');
                        },2000);
                    }else{
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                        $scope.isDisabled = false;
                    }
                });
            }
            
        }


        //表单验证
        function validate(){
            jQuery('form').validate({
               rules: {
               	    pname: {
                       required: true
                   },
                   bname: {
                       required: true
                   }
                   authNo: {
                       required: true
                   }
                   authType: {
                       required: true
                   }
                   endTime: {
                       required: true
                   }
               },
               messages: {
                	pname: {
                       required: '请填写姓名'
                    },
                   bname: {
                       required: '请填写房屋地址'
                   }
                   authNo: {
                   	required: '请填写门禁卡号'
                   }
                   authType: {
                   	required: '请选择卡类型'
                   }
                   endTime: {
                   	required: '请选择结束时间'
                   }
                   },
                submitHandler: function() {
                    $scope.submit();
                }
            });
        }
        
        
        $scope.showMemberList = function() {
                var modalInstance = $modal.open({
                    templateUrl : 'selUserTree.html',//script标签中定义的id
                    controller : 'selUserTreeModalCtrl',//modal对应的Controller
                    size: 'lg',
                    resolve : {
                    	items : function() {  
                            return $scope.items;  
                        }  
                    }
                });
                
                modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
                     
                });  
                modalInstance.result.then(function(result) { 
                	
                	var people = result;
                	var txtContent = "";
                	var ids = "";
                	for(var j =0;j<people.length;j++){
                		ids += people[j].personId + ","
                		txtContent += people[j].personName + "(" + people[j].IDCardNo + "),";
                	}
                	console.log(ids.substring(0,ids.length-1))
                	console.log(txtContent.substring(0,txtContent.length-1));
                	$scope.record.personId = ids.substring(0,ids.length-1);
                	$scope.people = txtContent.substring(0,txtContent.length-1);
                }, function(reason) {  
//                    console.log(reason);  
                }); 

            };
            
            $scope.openDevice = function() {
                var modalInstance = $modal.open({
                    templateUrl : 'dmDeviceList.html',//script标签中定义的id
                    controller : 'deviceModalCtrl',//modal对应的Controller
                    size: 'lg',
                    resolve : {
                    	items : function() {  
                            return $scope.items;  
                        }  
                    }
                });
                
                modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
                     
                });  
                modalInstance.result.then(function(result) { 
                	
                	$scope.bname=result[0];
                	$scope.record['deviceId']=result[1];
                }, function(reason) {  
                    console.log(reason);  
                }); 

            };

         //日期控件
         $scope.dateOptions = {
             startingDay: 1,
             class: 'datepicker'
         };
         //日期控件
         $scope.format = "yyyy-MM-dd";
         $scope.openDatepicker = function($event, i) {
             $event.preventDefault();
             $event.stopPropagation();
             if(i==1){
                 $scope.beginTime = true;
             }else if(i==2){
                 $scope.endTime = true;
             }
         }

    }]);


    app.controller('selUserTreeModalCtrl', function($scope, $rootScope, $modal,$modalInstance,items,toaster) {

    	$scope.getTreeList=function(){ 
         	 $.ajax({
     				type: 'POST',
     	            dataType: 'json',
     				contentType:'application/json;charset=UTF-8',
     				url : '/dac/regionManage/region/tree',
     				data: angular.toJson($scope.param)
     			}).then(callback);
         	 
         	 function callback(result) {
     			 
     			 if(result.httpCode ==200){//成功
                         $scope.angularTreeList=[];  
                         //查询树  
                         $scope.orgTypeList=result.data;    
                         //创建根节点  
                         var root={};  
                         root.regionname="区域列表";  
                         root.level=0;  
                         root.sequence=1;  
                         root.parentid=-1;
                         root.dragEnabled = false;
                         root.children=result.data;  
                         $scope.angularTreeList.push(root);  
                         // $scope.treeOptions.data=$scope.angularTreeList;
                         
                         $scope.$apply();
                        toaster.clear('*');
                        toaster.pop('success', '', "加载成功");
                     }else{
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                        $scope.isDisabled = false;
                     }
     			 
                 }
         	
         }  

        $scope.getTreeList();
       
       //树点击事件
       $scope.clickRegion = function(regionId,regionName){
       	$scope.regionName = regionName;
       	$scope.param.regionId=regionId;
       	$scope.search();
       }
    	
        $scope.param = { };
		$scope.search = function () {
	        $scope.loading = true;
	        if($scope.pageFlag != 1){//未点击翻页
			  	   $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
			  	}else{//点击翻页
			  	   $scope.pageFlag = 2;//将开关值设置为2
			  	}
			$.ajax({
				type: 'PUT',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/dac/userManager/list',
				data: angular.toJson($scope.param)
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.search();
		
        $scope.select = function(item) {  
        	$scope.params = [];
        	$scope.params[0]=item.address;
        	$scope.params[1]=item.deviceId;
            $modalInstance.close($scope.params);   
        };  
        $scope.cancel = function() {  
            $modalInstance.dismiss('cancel');  
        }; 
        
     // 翻页
        $scope.pagination = function (page) {
              $scope.param.pageNum=page;
              $scope.pageFlag = 1;//将开关值设置为1
              $scope.search();
        };
        
        $scope.isChecked = function(id){  
            return $scope.selected.indexOf(id) >= 0 ;  
        } ; 
        $scope.selectCheck = function(checkbox, id){
        	var checked = checkbox.checked ;  
            console.log(checked+','+id);
            if(checked){  
            	if($scope.selected.indexOf(id)==-1){
            		 $scope.selected.push(id) ;
            	}
            }else{  
                var idx = $scope.selected.indexOf(id) ;  
                $scope.selected.splice(idx,1) ;  
            }
            console.log($scope.selected);
        }
        $scope.selected = [] ; 
        $scope.clickCheck = function($event,id){
        	 var checkbox = $event.target ; 
        	 
        	 $scope.selectCheck(checkbox,id);
             
        }
        
        $scope.checkAll = function($event){
        	var checkbox = $event.target ; 
        	var checked = checkbox.checked ;
        	var checkBoxs = $('.tbody').find('input[type=checkbox]');
        	checkBoxs.each(function(i){
        		this.checked = checked;
        		$scope.selectCheck(this,$(this).data('id'));
        	});
        	
        }
        
        $scope.sure = function(){

        	$modalInstance.close($scope.selected);
        	
        }
        
        $scope.queryPerson = function(){
        	$scope.search();
        	$scope.$apply();
        }
        
    });
    
    
app.controller('deviceModalCtrl', function($scope, $rootScope, $modal,$modalInstance,items) {
        
        $scope.param = { };
		$scope.search = function () {
	        $scope.loading = true;
	        if($scope.pageFlag != 1){//未点击翻页
			  	   $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
			  	}else{//点击翻页
			  	   $scope.pageFlag = 2;//将开关值设置为2
			  	}
			$.ajax({
				type: 'POST',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/dac/dm/dmManager/list',
				data: angular.toJson($scope.param)
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.search();
		
        $scope.select = function(item) {  
        	$scope.params = [];
        	$scope.params[0]=item.address;
        	$scope.params[1]=item.deviceId;
            $modalInstance.close($scope.params);   
        };  
        $scope.cancel = function() {  
            $modalInstance.dismiss('cancel');  
        }; 
        
     // 翻页
        $scope.pagination = function (page) {
              $scope.param.pageNum=page;
              $scope.pageFlag = 1;//将开关值设置为1
              $scope.search();
        };
    });
 